Skip to content

Answer only the tagged message and mark speaker changes - #46

Merged
Zeptiny merged 1 commit into
mainfrom
fix/chat-context-framing
Nov 13, 2026
Merged

Zeptiny merged 1 commit into
mainfrom
fix/chat-context-framing

Conversation

@Zeptiny

@Zeptiny Zeptiny commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Problem

The bot answered more than it was asked, and lost track of who was talking. Example: the channel talks about topic Y with the bot, then moves on to topic X, then someone @mentions the bot to search something about X. The answer covers X and also brings Y back.

I replayed that scenario through the real ChatEngine and run_turn, with a fake LLM that only records the request. The search request looked like this:

system: … <conversa_em_andamento> Participantes: Ana (@ana). …
          A mensagem a que você deve responder agora é sempre a ÚLTIMA mensagem de usuário …
user:      [Por Ana] qual plugin de proteção de terreno vocês recomendam…
assistant: Pra survival, o GriefPrevention é o mais usado…
user:      [Mensagens no canal antes desta pergunta (channel_id=10)]
           …5 lines of chat about the new snapshot…
           [Por John (@john) • author_id=222 • …]
           pesquisa aí o que mudou no combate nessa snapshot

Two problems in how the request is built:

  • Nothing marks the chatter as background. The recent-messages window, or the chat since the last turn, sits in the same user message as the question. The only rule was "answer the LAST user message", and that message contains all the chatter. On a first turn, <mensagens_recentes_do_canal> isn't explained at all. The burst rule "responda a todas de uma vez" also fits the chatter, which is one message per line too.
  • Speaker changes are easy to miss. Every person is the user role, so the only sign of a new speaker is the name in the tag. The compact rendering marked it ("nova pessoa na conversa"), but verbatim replay (the last 6 turns) didn't. The participant list in <conversa_em_andamento> also leaves out anyone speaking for the first time (John above).

Changes

The chatter is background, and the bot answers only the tagged message

  • core/prompts.py: new CONTEXT_INSTRUCTIONS at the top of the chat and /ask instructions (so Spark gets it too). Everything before the [Por …] tag is prepared for the bot. It answers only what that person wrote. A replied-to message shows up under the tag as [Mensagem respondida — …]. Channel messages are for understanding the question, not to be answered, commented on or summarized unless asked. When the person changes topic, the bot follows the new one. <canal> and <memory> are described as "prepared for you", not "ignore", so channel instructions are still followed.
  • The burst rule now says the lines are the ones under the tag, and that the channel messages above it don't count.
  • core/context.py and core/conversations.py: the recent window opens with "(… — só contexto: não são pedidos para você.)". The gap header gets the same note: [Mensagens no canal antes desta pergunta (channel_id=…) — só contexto: …].

Speaker changes

  • core/conversations.py: new speaker_change(). author_tag() / build_current_message() take the previous turn's author and add outra pessoa — antes falava Ana (@ana) to the tag when it differs. run_turn passes ctx.history[-1]['author'].
  • The mark is written when the turn is captured, so verbatim replay keeps it and stays byte-identical. The compact rendering uses the same mark instead of "nova pessoa na conversa".
  • <conversa_em_andamento> now says "Participantes até agora". It explains the mark and says who is speaking now: the author of the last message's tag, who may not have spoken before.

Not in this PR

  • Mention-continue still joins any @mention within CHAT_MENTION_CONTINUE_WINDOW messages of a live answer to that conversation, from anyone and about anything. That's how John's question got Ana's conversation in the example. The model is now told someone else took over and to follow the current topic, but the old history is still replayed. Limiting the merge to the same person would be a separate change.
  • The [Mensagem respondida — …] quote still has no author_id.

Prompt cache

The system prompt changes, and so does the gap header in the compact rendering of older turns. That causes one prefix-cache miss per conversation after deploy. Turns stored before this keep their captured text, without the mark, and are still replayed byte-identically.

Tests

  • tests/test_attribution.py: the mark in the tag, and none for the same speaker, a missing previous author or one without an id. The mark survives verbatim replay unchanged and the compact rendering marks the same turns. The conversation block lists participants so far and names the current speaker.
  • New tests/test_context_framing.py: the instructions are in the chat, /ask and Spark prompts. The gap block and the recent window are labelled as context. A real run_turn request marks a new speaker and doesn't mark the same one.
  • Full suite: 229 passed. ruff check . is clean.

🤖 Generated with Claude Code

The channel chatter (the recent window, or the gap since the last turn)
rides in the same user message as the question, and nothing told the
model it was background: the conversation block even said to answer "the
last user message", which contains all of it. So the bot answered the
channel's other topics along with what it was asked.

- The system prompt (chat and /ask, so Spark too) now explains how a turn
  reads: what comes before the [Por …] tag is prepared for the bot, and it
  answers only what that person wrote; channel messages are there to
  understand the question, not to answer, comment on or summarize; when
  the person changes topic, follow the new one.
- The recent window and the gap header say they are only context. The
  burst rule now says the lines are the ones under the tag.

Every speaker is the `user` role, so a change of speaker was only visible
in the name inside the tag, and the participant list left out whoever was
speaking for the first time.

- The tag says "outra pessoa — antes falava …" when the speaker differs
  from the previous turn's. It is written when the turn is captured, so
  verbatim replay stays byte-identical; compact turns get the same mark
  (it replaces "nova pessoa na conversa").
- The conversation block lists participants "até agora" and says who is
  speaking now: the author of the last message's tag, who may be new.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 862cb902-ed61-40a9-93fb-92ababb698f3
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@Zeptiny
Zeptiny merged commit 8145811 into main Nov 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant